<script setup lang="ts">
import { computed } from "vue";

/**
 * @see {@link https://vuejs.org/api/sfc-script-setup.html#defineoptions}
 */

import {
  getSendLogsList
} from "@/api/smslog";
import { getDicts } from "@/store/modules/dict";
import { ParamsType, SmsSendLogDto } from "@/api/smslog/types";

import { useScreen } from "@/store/modules/screen";

const screenStore = useScreen();
const mobile = computed(() => screenStore.device);
const screenHeight = computed(() => screenStore.screenHeight);
const screenWidth = ref(600);



defineOptions({
  name: "User",
  inheritAttrs: false,
});
function initScreen() {
  if (mobile.value === "mobile") {
    screenWidth.value = screenStore.screenWidth;
  } else {
    screenWidth.value = 450;
  }
}

const queryFormRef = ref(ElForm); // 查询表单

const loading = ref(false);
const ids = ref([]);
const total = ref(0);

const queryParams = reactive<ParamsType>({
  pageIndex: 1,
  pageSize: 10,
});
const userList = ref<SmsSendLogDto[]>();


/**
 * 查询
 */
function handleQuery() {
  loading.value = true;
  getSendLogsList(queryParams)
    .then(({ data }) => {
      userList.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/**
 * 重置查询
 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageIndex = 1;
  queryParams.msgType=undefined;
  handleQuery();
}

/**
 * 行checkbox change事件
 */
function handleSelectionChange(selection: any) {
  ids.value = selection.map((item: any) => item.userId);
}

const smsMsgTypes = getDicts("smsMsgType");
onMounted(() => {
  initScreen();

  handleQuery(); // 初始化用户列表数据
});
</script>

<script lang="ts">
export default {
  name: "User",
};
</script>

<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="关键字" prop="keywords">
          <el-input
            v-model="queryParams.keywords"
            placeholder="请输入关键字"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <!-- <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="全部" clearable style="width: 200px">
            <el-option label="正常" value="1" />
            <el-option label="离岗" value="2" />
            <el-option label="封禁" value="3" />
          </el-select>
        </el-form-item> -->
        <el-form-item label="消息类型">
          <el-select v-model="queryParams.msgType" placeholder="全部" clearable>
            <el-option v-for="item in smsMsgTypes" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery"><i-ep-search />搜索</el-button>
          <el-button @click="resetQuery">
            <i-ep-refresh />
            重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <el-table
        v-loading="loading"
        :data="userList"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
  
        <el-table-column key="phone" label="手机号码" align="center" width="150" prop="phone" />
        <el-table-column key="msgTypeName" label="消息类型" align="center" width="150" prop="msgTypeName" />
        <el-table-column key="msgContent" label="消息内容" width="600" align="center" prop="msgContent" />
           <el-table-column label="状态" align="center" width="100">
          <template #default="scope">
            <el-tag v-if="scope.row.status === 1" type="success">发送成功</el-tag>
            <el-tag v-if="scope.row.status === 0" type="warning">发送失败</el-tag>
          </template>
        </el-table-column>
        <el-table-column key="verificationCode" label="验证码" width="100" align="center" prop="verificationCode" />
        <el-table-column key="resultMsg" label="返回内容" align="center" prop="resultMsg" />    
        <el-table-column label="发送时间" width="150" align="center" prop="createTime"></el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageIndex"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>

  </div>
</template>
<style lang="scss" scoped>
.el-form-item__label {
  width: 100px !important;
}

.input300 {
  width: 210px !important;
}
</style>
